class MyMap[A, B](defaultfun:(A)=>Option[B]) extends scala.collection.mutable.HashMap[A,B]{
  override def default(key:A)={
    val r=defaultfun(key)
    if(r==None) throw new NoSuchElementException("key not found: " + key)
    this(key)=r.get
    r.get
  }
}
object Util{
  val password = io.Source.fromFile("pwd.txt").getLines.next
  def sendmail(subject:String,text:String){
    import java.util.Properties
    import javax.mail._
    import javax.mail.internet._
  
    val username = "ghjlxw@gmail.com"
  
    val props = new Properties
    props.put("mail.smtp.auth", "true")
    props.put("mail.smtp.starttls.enable", "true")
    props.put("mail.smtp.host", "smtp.gmail.com")
    props.put("mail.smtp.port", "587")
  
    val session = Session.getInstance(props,new javax.mail.Authenticator {
      override def getPasswordAuthentication()= new PasswordAuthentication(username, password)
    })
    
    val message = new MimeMessage(session)
    message.setFrom(new InternetAddress(username))
    message.setRecipients(javax.mail.Message.RecipientType.TO,"7408331@qq.com")
    message.setSubject("^&"+subject)
    message.setText(text)
    
    Transport.send(message)
  }
  def fromXZFile(name:java.io.File)={
    var infile = new java.io.FileInputStream(name)
    var inxz = new org.tukaani.xz.XZInputStream(infile)
    val ret=io.Source.fromInputStream(inxz).getLines.toList
    inxz.close
    infile.close
    ret
  }

  def fromXZFile(name:String):List[String]=fromXZFile(new java.io.File(name))

  def saveXZFile(name:String,lines:Seq[String]){
    var outfile = new java.io.FileOutputStream(name)
    var outxz = new org.tukaani.xz.XZOutputStream(outfile, new org.tukaani.xz.LZMA2Options())

    val writer = new java.io.PrintWriter(outxz)
    lines.foreach(writer.println)
    writer.close
    outxz.close
    outfile.close
  }
  def save(file:String,s:TraversableOnce[String],append:Boolean=false){
  	val pdir=new java.io.File(file).getParentFile
    if(pdir!=null && !pdir.exists) pdir.mkdirs
    val filestream=new java.io.FileOutputStream(file,append)
    val writer1 = new java.io.OutputStreamWriter(filestream,"UTF-8")
    val writer = new java.io.PrintWriter(writer1)
    s.foreach(writer.println)
    writer.close
    writer1.close
    filestream.close
  }
}